
/**
 * 复制到剪切板
 * @param {String|DOMElement} el 包含要复制内容的input或textarea的 选择器字符串或DOM元素
 */
const CopyToClipboard = el => {
  // resolve the element
  el = (typeof el === 'string') ? document.querySelector(el) : el

  el.select()

  // handle iOS as a special case
  if (navigator.userAgent.match(/ipad|ipod|iphone/i)) {
    // save current contentEditable/readOnly status
    var editable = el.contentEditable
    var readOnly = el.readOnly

    // convert to editable with readonly to stop iOS keyboard opening
    el.contentEditable = true
    el.readOnly = true

    // create a selectable range
    var range = document.createRange()
    range.selectNodeContents(el)

    // select the range
    var selection = window.getSelection()
    selection.removeAllRanges()
    selection.addRange(range)
    el.setSelectionRange(0, 999999)

    // restore contentEditable/readOnly to original state
    el.contentEditable = editable
    el.readOnly = readOnly
  }
  // else {
  //   el.select()
  // }

  // execute copy command
  document.execCommand('copy')
}

export { CopyToClipboard }
